<?php
namespace app\first\controller;

use app\first\model\ArticleOrder;
use app\first\model\Collection;
use app\first\model\HomeCom;
use app\first\model\HomeComView;
use app\first\model\Keywords;
use think\db\Where;

/**
 * Class Article
 * @title 内容相关
 * @url /v1/article
 * @desc  获取内容相关
 * @version 1.0
 * @package app\first\article
 * @route('v1/article')
 */
class Article extends Base{
    //是否开启授权认证,默认开启
    public $apiAuth = false;
    protected $model = null;
    protected $validate = null;
    /*
    * 需要附加的其他方法,比如:topList,newList
    * protected $extraActionList = ['topList','newList',...];
    * 已经对核心的类进行了重写,扩展的新方法会自动添加到认证中,无需再手动书写
    */
    protected $extraActionList = [];

    protected $skipAuthActionList= ['banner','list','benefit',''];

    public function __construct(){
        parent::__construct();
        $this->model = new \app\first\model\Article();
        $this->validate = new \app\first\validate\Article();
    }

    /**
     * @title 获取类目列表
     * @method index
     * @param int $cate_id 一级栏目 false
     * @param int $first_cate_id 一级栏目 false
     * @param int $twice_cate_id 二级栏目 false
     * @param string $city_id 城市id true
     * @param int $is_top 是否置顶 false 0 1是,0否
     * @param int $limit 每页条数 true 8
     * @param int $page 当前页数 true 1
     * @route('v1/article/list','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\exception\DbException
     */
    public function index($cate_id=0,$first_cate_id=0,$twice_cate_id=0,$city_id='',$is_top=0,$limit=8,$page = 1){
        $map = [
            ['status','eq',2],
            ['cid_type','eq',1]
        ];
        if($cate_id){
            array_push($map,['cid_type','eq',$cate_id]);
        }
        if($first_cate_id){
            array_push($map,['first_cate_id','eq',$first_cate_id]);
        }
        if($twice_cate_id){
            array_push($map,['twice_cate_id','eq',$twice_cate_id]);
        }
        if($city_id){
            array_push($map,['city_id','eq',$city_id]);
        }
        $order= $is_top?"is_top desc,create_time desc":'create_time desc';
        $list = $this->model->getList($map,$order,$limit);
        return $this->sendSuccess($list);
    }

    /**
     * @title 获取优惠列表
     * @method benefit
     * @param string $city_id 城市id true
     * @param int $is_top 是否置顶 false 0 1是,0否
     * @param int $limit 条数 false 8
     * @param int $page 当前页数 true 1
     * @route('v1/article/benefit','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\exception\DbException
     */
    public function benefit($city_id='',$is_top=1,$limit=8){
        $map = [
            ['status','eq',2],
            ['cid_type','eq',2],
//            ['is_payed','eq',1],
        ];
        if($city_id){
            array_push($map,['city_id','eq',$city_id]);
        }
        $order= $is_top?"is_top desc,create_time desc":'create_time desc';
        $list = $this->model->getList($map,$order,$limit);
        return $this->sendSuccess($list);
    }


    /**
     * @title 获取资讯列表
     * @method news
     * @param string $city_id 城市id true
     * @param int $is_top 是否置顶 false 0 1是,0否
     * @param int $limit 条数 false 8
     * @param int $page 当前页数 true 1
     * @route('v1/article/news','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\exception\DbException
     */
    public function news($city_id='',$is_top=1,$limit=8){
        $map = [
            ['status','eq',2],
            ['cid_type','eq',3]
        ];
        if($city_id){
            array_push($map,['city_id','eq',$city_id]);
        }
        $order= $is_top?"is_top desc,create_time desc":'create_time desc';
        $list = $this->model->getList($map,$order,$limit);
        return $this->sendSuccess($list);
    }

    /**
     * @title 获取详情
     * @method read
     * @param int $id id true
     * @route('v1/article/read','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function read($id=0){
        $info = $this->model->getOneById($id);
        $info->hidden(['update_time','sort']);
        return $this->sendSuccess($info);
    }

    /*
    /**
     * @title 删除
     * @method delete
     * @param int $id 资源id true
     * @route('v1/article/delete','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     * @throws \Exception
    public function delete($id = 0){
        $info = $this->model->get($id);
        if(!$info->delete()){
            return $this->sendError(0,'删除失败');
        }
        return $this->sendSuccess('','删除成功');
    }
     */

    /**
     * @title 获取轮播
     * @method banner
     * @param int $limit 条数 false 5
     * @route('v1/article/banner','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function banner($limit=5){
        $carousel = new \app\first\model\Carousel();
        $list = $carousel->getList('sort asc,create_time desc',$limit);
        return $this->sendSuccess($list);
    }

    /**
     * @title 获取随机相关
     * @method like
     * @param int $cate_id 栏目id true 17
     * @param int $limit 条数 true 4
     * @route('v1/article/like','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function like($cate_id=0,$limit=4){
        $article = new \app\first\model\Article();
        $list = $article->likes($cate_id,$limit);
        return $this->sendSuccess($list);
    }

    /**
     * @title 获取最新发布
     * @method last_publish
     * @param int $city_id 城市id true
     * @param int $cate_id 类型 true 0 1类目,2优惠,3资讯,0所有
     * @param int $limit 条数 false 10
     * @route('v1/article/last_publish','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function last_publish($city_id=0,$cate_id=0,$limit=10){
        $article = new \app\first\model\Article();
        $list = $article->lastList($cate_id,$city_id,'create_time desc',$limit);
        return $this->sendSuccess($list);
    }

    /**
     * @title 搜索
     * @method search
     * @param string $q 关键词 true
     * @param int $cid 类型 true 1 1类目,2优惠,3快讯
     * @param int $first_cate_id 一级栏目 false 0 0不限,其他指定
     * @param int $twice_cate_id 二级栏目 false 0 0不限,其他指定
     * @param int $limit 条数 true 10
     * @param int $page 页数 true 1
     * @route('v1/article/search','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     * @throws \think\exception\DbException
     */
    public function search($q='',$cid=1,$first_cate_id=0,$twice_cate_id=0,$limit=10,$page=1){
        $article = new \app\first\model\Article();
        if(!$q){
            return $this->sendError(0,'请输入关键词');
        }
        $where = new Where();
        $where['status']=2;
        $where['cid_type']=$cid;
        if($first_cate_id){
            $where['first_cate_id']=$first_cate_id;
        }
        if($twice_cate_id){
            $where['twice_cate_id']=$twice_cate_id;
        }
        $keywords = new Keywords();
        $keywords->addNew($q);
        $list = $article->search($q,$where,$limit);
        return $this->sendSuccess($list,'');
    }

    /**
     * @title 获取热门关键词
     * @method hot_keywords
     * @param int $limit 条数 false 10
     * @route('v1/article/hot_keywords','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function hot_keywords($limit=10){
        $keywords = new Keywords();
        $list = $keywords
            ->field('id,keywords')
            ->where('type','eq',1)
            ->limit($limit)
            ->order('hits desc,create_time desc')
            ->select();
        return $this->sendSuccess($list);
    }

    /**
     * @title 首页特惠推荐
     * @method home_com
     * @param int $limit 条数 false 10
     * @param boolean $flag 开启失效 false
     * @route('v1/article/home_com','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\exception\DbException
     */
    public function home_com($limit=10,$flag=false){
        $com = new HomeCom();
        $where = [];
        if($flag){
            $where[]=['create_time','lt',time()];
        }
        $list = $com->getList($where,'create_time desc',$limit);
        return $this->sendSuccess($list);
    }

    /**
     * @title 首页特惠推荐浏览记录
     * @method home_com_view
     * @param int $aid 文章id true
     * @route('v1/article/home_com_view','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     */
    public function home_com_view($aid=0){
        $com = new HomeComView();
        $result = $com->addNew($aid,self::$uid,$out);
        if(!$result){
            return $this->sendError($out);
        }
        return $this->sendSuccess('操作成功');
    }

    /**
     * @title 创建订单
     * @method create_order
     * @param string $title 标题 true
     * @param string $aid 文章id true
     * @param int $totals 价格 true
     * @param int $discount 优惠
     * @param string $info 介绍
     * @param int $type 类型 false 1 1微信,2支付宝
     * @route('v1/article/create_order','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     */
    public function create_order($title='',$aid='',$totals=0,$discount=0,$info='',$type=1){
        $order = new ArticleOrder();
        $result = $order->addNew(self::$uid,$title,$aid,$info,$totals,$discount,$out);
        if(!$result){
            return $this->sendError($out);
        }
        $payment = (new Payment())->signature($type,'',$out['order_id'],$totals,$title);
        return $this->sendSuccess(array_merge($payment,$out));
    }

    /**
     * @param string $title
     * @param string $aid
     * @param int $totals
     * @param int $discount
     * @param string $info
     * @param int $type
     * @param array $data
     * @return bool
     */
    public function create_order1($title='',$aid='',$totals=0,$discount=0,$info='',$type=1,&$data=[]){
        $order = new ArticleOrder();
        $result = $order->addNew(self::$uid,$title,$aid,$info,$totals,$discount,$out);
        if(!$result){
            $data = $out;
            return false;
        }
        $payment = (new Payment())->signature($type,'',$out['order_id'],$totals,$title);
        $data = array_merge($payment,$out);
        return  false;
    }
}